/*
 * Filename:    AuditManageRollBackServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-1-7
 */
package com.paic.mhis.hcpms.audit.biz.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditManageRollBackService;
import com.paic.mhis.hcpms.audit.biz.service.AuditWorkFlowService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditManageRollBackDTO;
import com.paic.mhis.hcpms.audit.dto.AuditProblemsDTO;
import com.paic.mhis.hcpms.audit.util.AuditWfContentsUtil;
import com.paic.mhis.hcpms.audit.util.EventCodeUtil;
import com.paic.mhis.hcpms.common.storage.biz.service.StorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDataDTO;

/**
 * 稽核撤回业务层实现类
 * 
 * @author EX-PANAN001
 * 
 */
@Service("auditManageRollBackService")
public class AuditManageRollBackServiceImpl implements
		AuditManageRollBackService {

	@Autowired
	private CommonDAO CommonDAO;

	@Autowired
	private AuditWorkFlowService auditWorkFlowService;

	@Autowired
	private StorageService StorageService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public AuditManageRollBackDTO executeRollBack(AuditManageRollBackDTO rt,
			UserDetails user) throws Exception {
		if (rt.getType() == null || rt.getStatus() == null) {
			rt.setMessage("");
			return rt;
		}
		// 事件 处理
		if (EventCodeUtil.industry_type1.equals(rt.getType())) {
			HcpmsStorageDataDTO hdo = new HcpmsStorageDataDTO();
			AuditProblemsDTO ad = new AuditProblemsDTO();
			ad.setProblemCode(rt.getCode());
			ad = (AuditProblemsDTO) CommonDAO.get(
					"auditProblemsDao.queryAuditProblemsDetail", ad);
			if (ad.getProblemStatus().equals(rt.getStatus())) {
				// 工作流方法
				if ("3".equals(rt.getStatus())) {
					// 从审核撤回至首节点 暂无工作流方法
					if (ad.getOtherCaseCode() != null) {
						auditWorkFlowService.processWorkFlow(rt.getCode(),
								user, user.getUsername(), null,
								AuditConstants.TASK_TYPE_PROBLEM,
								AuditWfContentsUtil.N2014101_FIRST_NODE);
					} else {
						// 这里特殊 状态为草稿
						auditWorkFlowService.deleteWorkFlow(rt.getCode(),
								AuditConstants.TASK_TYPE_PROBLEM);
						ad = new AuditProblemsDTO();
						ad.setProblemCode(rt.getCode());
						ad.setProblemStatus("0");
						CommonDAO.update(
								"auditProblemsDao.updateAuditProblems", ad);
						rt.setMessage(rt.getCode() + "撤回成功！");
						hdo.setBusinessId(rt.getCode());
						StorageService.deleteStorageDate(hdo);
						return rt;
					}
				} else {
					auditWorkFlowService.processWorkFlow(rt.getCode(), user,
							user.getUsername(), null,
							AuditConstants.TASK_TYPE_PROBLEM,
							(String) AuditWfContentsUtil.EVENT_STATUS_NODE
									.get(rt.getStatus()));

				}
				ad = new AuditProblemsDTO();
				ad.setProblemCode(rt.getCode());
				ad.setProblemStatus((String) EventCodeUtil.EVENT_STATUS.get(rt
						.getStatus()));
				CommonDAO.update("auditProblemsDao.updateAuditProblems", ad);
				hdo.setBusinessId(rt.getCode());
				StorageService.deleteStorageDate(hdo);
				rt.setMessage(rt.getCode() + "撤回成功！");
			} else {
				rt.setMessage("事件不可撤回，已经流转到下一节点！");
				return rt;
			}
		} else if (EventCodeUtil.industry_type2.equals(rt.getType())) {
			// 案件 处理
			AuditCaseDTO ac = new AuditCaseDTO();
			ac.setCaseCode(rt.getCode());
			ac = (AuditCaseDTO) CommonDAO.get("audticase.getCase", ac);
			if (ac.getCaseState().equals(rt.getStatus())) {
				HcpmsStorageDataDTO hdo = new HcpmsStorageDataDTO();
				ac = new AuditCaseDTO();
				ac.setCaseCode(rt.getCode());
				ac.setCaseState((String) EventCodeUtil.CASE_STATUS.get(rt
						.getStatus()));
				CommonDAO.update("audticase.updateCaseStatus", ac);
				// wf
				auditWorkFlowService.processWorkFlow(rt.getCode(), user, user
						.getUsername(), null, AuditConstants.TASK_TYPE_CASE,
						(String) AuditWfContentsUtil.CASE_STATUS_NODE.get(rt
								.getStatus()));
				rt.setMessage(rt.getCode() + "撤回成功！");
				hdo.setBusinessId(rt.getCode());
				StorageService.deleteStorageDate(hdo);
			} else {
				rt.setMessage("案件不可撤回，已经流转到下一节点！");
				return rt;
			}
		}
		// 删除暂存信息

		return rt;
	}

}
